package com.hspedu.newData.LCR;
/**
 * @Author: yh
 * @description: 
 * @CreateTime: 2025-05-28 
 * @Version: 1.0
 */

public class LCR116省份数量 {

    public int findCircleNum(int[][] isConnected) {

        int n = isConnected.length;

        int res = 0;
        boolean[] visited = new boolean[n];
        for (int i = 0; i < n; i++) {
            if (!visited[i]){
                res++;
                dfs(isConnected,i,visited);
            }
        }
        return res;
    }

    public void dfs(int[][] isConnected,int key,boolean[] visited){
        if (visited[key]){
            return;
        }
        visited[key] = true;
        int[] ints = isConnected[key];
        for (int i = 0; i < ints.length; i++) {
            if (ints[i] == 1 && key != i){
                dfs(isConnected,i,visited);
            }
        }
    }

}
